<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace plugins\Express\controller;

use app\common\builder\ZBuilder;
use app\common\controller\Common;
use plugins\Express\model\Express;
use plugins\Express\validate\Express as ExpressValidate;


class Admin extends Common
{
    /**
     * 插件管理页
     */
    public function index()
    {

        if($this->request->isPost()){

            $this->store();
        }
        $data = Express::getRes();

        return ZBuilder::make('form')
            ->addFormItems([
                ['text','app_id','用户id'],
                ['text','api_key','加密密钥'],

            ])
            ->setFormData($data)
            ->fetch();
    }

    public function store()
    {
        $data = input('post.');
        unset($data['__token__']);
        $validate = new ExpressValidate();
        if (!$validate->check($data)) {
            // 验证失败 输出错误信息
            return $this->error($validate->getError());
        }
        $res = Express::saveSet($data);
        if($res){
            $this->success('保存成功',url('index'));
        }else{
            $this->error('保存失败!');
        }
    }

}
